package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import one.stand.model.CompanyCouponModel;
import one.stand.model.ResultModel;
import one.stand.model.UserCouponModel;
import one.stand.request.UserCouponRequest;
import one.stand.response.UserCouponResponse;
import one.stand.service.UserCouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/6/24 17:26
 */
@Service
public class UserCouponServiceImpl extends PublicBaseService implements UserCouponService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel<Boolean> receive(UserCouponRequest request) {
        if (null == request.getCouponId()) {
            return ResultModel.checkParamFail();
        }
        CompanyCouponModel outModel = companyCouponMapper.selectByPrimaryKey(request.getCouponId());
        if (null == outModel) {
            log.info("优惠券未获取到数据。。。。。");
            return ResultModel.fail("抱歉，优惠券已经领取完了");
        }
        if (outModel.getGrantEndTime().getTime() < currentDate().getTime()) {
            log.info("当前不止优惠券领取时间范围内。。。。");
            return ResultModel.fail("抱歉，您来晚了一步!");
        }
        outModel.setSurplusNum(outModel.getSurplusNum() - outModel.getReceiveNum());
        companyCouponMapper.updateByPrimaryKeySelective(outModel);
        log.info("修改优惠券剩余数量。。。。。");
        UserCouponModel inModel = new UserCouponModel();
        inModel.setUserId(request.getId().intValue());
        inModel.setCouponId(request.getCouponId());
        inModel.setNum(outModel.getReceiveNum());
        inModel.setCreateTime(currentDate());
        int insertRes = userCouponMapper.insertSelective(inModel);
        if (insertRes <= 0) {
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<List<UserCouponResponse>> myList(UserCouponRequest request) {
        UserCouponModel userCouponModel = new UserCouponModel();
        userCouponModel.setUserId(request.getId().intValue());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<UserCouponResponse> list = userCouponMapper.selectListByUserId(userCouponModel);
        return ResultModel.success(list);
    }

    @Override
    public ResultModel<List<UserCouponResponse>> noReceive(UserCouponRequest request) {
        if (null == request.getCompanyId()) {
            return ResultModel.checkParamFail();
        }
        UserCouponModel userCouponModel = new UserCouponModel();
        userCouponModel.setCompanyId(request.getCompanyId());
        userCouponModel.setUserId(request.getId().intValue());
        List<UserCouponModel> list = userCouponMapper.selectListByCompanyIdAndNotInCouponId(userCouponModel);
        List<UserCouponResponse> responseList = new ArrayList<>();
        list.forEach(l -> {
            {
                UserCouponResponse response = new UserCouponResponse();
                BeanUtils.copyProperties(l, response);
                responseList.add(response);
            }
        });
        return ResultModel.success(responseList);
    }
}
